package conductores;

import java.util.ArrayList;

import aima.search.framework.Successor;

public class OpSwap implements IOperator {

	@Override
	public void execute(Board board, int condO, Conductor conductorOrigen,
			ArrayList<Object> successors) {
		boolean zero = false;
		for (int i = 0; i < conductorOrigen.getRuta().size(); i++) {
			int usernum = conductorOrigen.getRuta().get(i);
			if (usernum > 0) {
				Swap(board, condO, usernum, conductorOrigen, successors);
			} else if (!zero && usernum == 0) {
				zero = true;
				Swap(board, condO, usernum, conductorOrigen, successors);
			}
		}
	}

	private void Swap(Board board, int condO, int usernum,
			Conductor conductorOrigen, ArrayList<Object> successors) {

		Conductor conductorClone = conductorOrigen.clone();
		conductorClone.removeUser(usernum);

		for (int i = 0; i <= conductorClone.getRuta().size(); i++) {
			for (int j = i; j <= conductorClone.getRuta().size(); j++) {
				Conductor conductorVar = conductorClone.clone();
				conductorVar.addUser(usernum, i, j);
				if (conductorVar.ValidarRuta()) {
					Board newboard = board.clone();
					newboard.getConductores().set(condO, conductorVar);
					String s = "Cambiamos la ruta del conductor " + condO
							+ " el usuario " + usernum
							+ " se recoge en el orden " + i
							+ " y se deja en el orden " + j;
					successors.add(new Successor(s, newboard));
				}
			}
		}
	}
}
